package com.zhang.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.entity.VisitCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Random;

@Service
public class ScheduleService {

    @Autowired
    VisitCountService visitCountService;

    //定时任务，每天中午12点触发，创建当天访问记录
    @Scheduled(cron = "0 0 12 * * ?")
    public void createVisitCount(){
        VisitCount visitCount = visitCountService.getOne(new QueryWrapper<VisitCount>().eq("date", LocalDate.now()));
        if(visitCount == null){
            Integer random = (new Random()).nextInt(12)+2;
            visitCount = new VisitCount();
            visitCount.setCount(0).setDate(LocalDate.now()).setRandom(random);
            visitCountService.save(visitCount);
        }
    }
}
